//
//  LHQHotViewController.m
//  Music
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LHQHotViewController.h"
#import "LHQLikeTableViewCell.h"
#import "LHQlikeModel.h"
#import "LHQMusicOnViewController.h"
@interface LHQHotViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,retain)NSMutableArray  *dataSource;
@property(nonatomic,retain)UITableView  *tableView;
@property(nonatomic,retain)UIImageView *head;
@property(nonatomic,assign)NSInteger  num;
@property(nonatomic,retain)NSString  *share;
@property(nonatomic,retain)NSString  *favorite;
@property(nonatomic,retain)UIView  *headView;
@end

@implementation LHQHotViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.tabBarController.tabBar.hidden = YES;
    
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataSource = [NSMutableArray  array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor  whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.title = self.title;
    self.navigationController.navigationBar.barStyle = 1;
    self.navigationController.navigationBar.tintColor = [UIColor  whiteColor];
    [self getListerdataSource];
}
-(void)getListerdataSource
{
    NSString  *URL = [NSString  stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%ld?app=ttpod&v=v9.0.2.2016052421&user_id=0&mid=iPhone8%%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.2&active=1&net=2&openudid=714e0317ebb947064241a6abe30a1fc639430b24&idfa=F8C6B2DF-27B0-4A7B-9E83-A86B4F61321F&utdid=Vw2RqCpjWHwDAAGyCA35RHh7&alf=(null)&bundle_id=com.ttpod.music&latitude=38.88327772352431&longtitude=121.5445855034722&access_token=(null)&vc=9000200",self.hotNumber];
    [MyTools  getNetWithUrl:URL body:nil header:nil response:LHQJSON success:^(id result) {
        NSDictionary  *Dict = (NSDictionary  *)result;
        self.share = Dict[@"share_count"];
        self.favorite =Dict[@"favorite_count"];
        NSMutableArray  *TEMP = Dict[@"songs"];
        for (NSDictionary *dic in TEMP) {
            LHQlikeModel  *model = [[LHQlikeModel  alloc]initWithDic:dic];
            [self.dataSource  addObject:model];
        }
        
        [self tableViewSubView];
        
        [self.tableView  reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}
-(void)tableViewSubView
{
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screenwidth, Screenheight / 3.51)];
    _headView.backgroundColor = [UIColor clearColor];
    self.head = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Screenwidth, _headView.frame.size.height)];
    [_headView addSubview:self.head];
    LHQlikeModel  *model = [self.dataSource  objectAtIndex:0];
    NSURL *url = [NSURL URLWithString:model.picUrl];
    [self.head sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    
    
    
    self.tableView = [[UITableView  alloc]initWithFrame:CGRectMake(0, 0, Screenwidth, Screenheight-64) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableHeaderView = _headView;
    [self.tableView  registerClass:[LHQLikeTableViewCell  class] forCellReuseIdentifier:@"recommend"];
    [self.view  addSubview:_tableView];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return   self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHQLikeTableViewCell  *Cell = [tableView  dequeueReusableCellWithIdentifier:@"recommend"];
    Cell.model = [self.dataSource  objectAtIndex:indexPath.row];
   self.num = indexPath.row +1;
    Cell.numLabel.text = [NSString  stringWithFormat:@"%ld",_num]  ;
    return Cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screenwidth, 40)];
    
    UIImageView  *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(70, 0, 20, 20)];
    imageView.image = [UIImage  imageNamed:@"音乐未收藏"];
    [header  addSubview:imageView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y+imageView.frame.size.height, 30, 20)];
    lable.textColor = [UIColor colorWithRed:0.49 green:0.50 blue:0.50 alpha:1.00];
    lable.text = self.share.description;
    lable.font = [UIFont  fontWithName:@"Helvetica-Oblique" size:10];
    [header addSubview:lable];
    
    
    UIImageView  *share = [[UIImageView alloc]initWithFrame:CGRectMake(300, 0, 20, 20)];
    share.image = [UIImage  imageNamed:@"音乐分享"];
    [header  addSubview:share];
    
    UILabel *sharelabel = [[UILabel alloc]initWithFrame:CGRectMake(share.frame.origin.x, share.frame.origin.y+share.frame.size.height, 30, 20)];
    sharelabel.textColor = [UIColor colorWithRed:0.49 green:0.50 blue:0.50 alpha:1.00];
    sharelabel.text = self.favorite.description;
    sharelabel.font = [UIFont  fontWithName:@"Helvetica-Oblique" size:10];
    [header addSubview:sharelabel];
    return header;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
       LHQlikeModel  *model = self.dataSource[indexPath.row];
        NSString  *url = model.urlList[1][@"url"];
         NSString  *time = model.auditionList[1][@"duration"];
        LHQMusicOnViewController  *on = [[LHQMusicOnViewController  alloc] init];
        
        on.image = model.picUrl;
        on.title = model.name;
        on.url = url;
        on.DataSource = self.dataSource;
        on.index = indexPath.row;
        on.time = time.description;
        on.urlid = model.singerName;
        [self.navigationController  pushViewController:on animated:YES];
        
    }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
